package com.mmcb.controller;

import com.hzit.common.result.R;
import com.mmcb.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * 作者：李嘉浩
 * 时间：2024-09-04-11:28
 * 功能：
 */
@RestController
@RequestMapping("/api/oss")
@Api("文件上传")
public class FileController {

    @Autowired
    private FileService fileService;

    @PostMapping("/file/upload")
    @ApiOperation("上传文件")
    public R upload(@RequestBody @ApiParam(value = "需要上传的文件",required = true) MultipartFile file,String module) throws IOException {
        InputStream inputStream = file.getInputStream();
        String originalFilename = file.getOriginalFilename();
        String url=fileService.upload(inputStream,originalFilename,module);
        return R.ok().data("url",url);
    }

    @DeleteMapping("/file/remove")
    public R remove(String url){
        fileService.remove(url);
        return R.ok().message("删除成功");
    }
}
